/**
 * Created by 荆安喜 on 2017/9/5.
 */
class batch {
    constructor(id) {
        this.id=id
        this.seletId = new Set()
        var that = this
        if ($("input[type='checkbox'],input[type='radio']").length > 0) {
            $("input[type='checkbox'],input[type='radio']").not(".toggle input[type='checkbox']").iCheck({
                checkboxClass: 'icheckbox',
                radioClass: 'iradio',
            }).on('ifClicked', function (event) {
                var id = $(event.target).attr('id')
                //全选与反选
                if (id == that.id) {
                    if (!$(event.target).prop('checked')) {
                        $('.A1').iCheck('check')
                        that.addId(that.id)
                    } else {
                        $('.A1').iCheck('uncheck')
                        that.delId(that.id)
                    }
                } else {
                    if (!$(event.target).prop('checked')) {
                        that.addId($(event.target).attr('data-id'))
                    } else {
                        that.delId($(event.target).attr('data-id'))
                    }
                }
            });
        }
    }

    addId(id) {
        var that = this
        if (id == that.id) {
            $('.A1').each(function (d, i) {
                that.seletId.add($(i).attr('data-id'))
            })
        } else {
            that.seletId.add(id)
        }
    }

    delId(id) {
        var that = this
        if (id == that.id) {
            $('.A1').each(function (d, i) {
                that.seletId.delete($(i).attr('data-id'))
            })
        } else {
            that.seletId.delete(id)
        }
    }

    delete(DOMId,callback) {
        var that = this
        $('#' + DOMId).off('click').on('click', function () {
            var ids = []
            that.seletId.forEach(function (d, i) {
                ids.push(d)
            })
            if (ids.length == 0) {
                return _as.jAlert("<p>请选择对象</p>")
            }
            var parms = {
                ids: ids
            }
            callback(parms)
        })
    }

    restart(DOMId, callback) {
        var that = this
        $('#' + DOMId).off('click').on('click', function () {
            var ids = []
            that.seletId.forEach(function (d, i) {
                ids.push(d)
            })
            if (ids.length == 0) {
                return _as.jAlert("<p>请选择对象</p>")
            }
            var parms = {
                ids: ids
            }
            callback(parms)
        })
    }

    close(DOMId, callback) {
        var that = this
        $('#' + DOMId).off('click').on('click', function () {
            var ids = []
            that.seletId.forEach(function (d, i) {
                ids.push(d)
            })
            if (ids.length == 0) {
                return _as.jAlert("<p>请选择对象</p>")
            }
            var parms = {
                ids: ids
            }
            callback(parms)
        })
    }
}